#!/usr/bin/env python
# encoding: utf-8

import wx
import json

charset = 'utf-8'
class MainWindow(wx.Frame):
    """We simply derive a new class of Frame."""
    def __init__(self, parent, title):
        wx.Frame.__init__(self, parent, title = title, size=(600, 600))
        # panel = wx.Panel(self)
        layout_sizer = wx.BoxSizer(wx.HORIZONTAL)
        self.source_control = wx.TextCtrl(self, size=(250,250), style=wx.TE_MULTILINE|wx.TE_PROCESS_ENTER)
        self.target_control=wx.TextCtrl(self, size=(250,250), style=wx.TE_MULTILINE|wx.TE_PROCESS_ENTER)

        # self.source_control = wx.TextCtrl(panel)
        # self.target_control = wx.TextCtrl(panel)

        self.json_format_btn = wx.Button(self, -1, '格式化JSON')
        self.xml_format_btn = wx.Button(self, -1, '格式化XML')

        self.format_json = ''

        button_sizer = wx.BoxSizer(wx.VERTICAL)
        button_sizer.Add(self.json_format_btn, 0, wx.ALIGN_CENTER|wx.ALIGN_CENTER_VERTICAL|wx.ALL, 1)
        button_sizer.Add(self.xml_format_btn, 0, wx.ALIGN_CENTER|wx.ALIGN_CENTER_VERTICAL|wx.ALL, 2)

        layout_sizer.Add(self.source_control, 0, wx.ALIGN_LEFT|wx.EXPAND|wx.ALL, 1)
        layout_sizer.Add(button_sizer, 0, wx.ALIGN_CENTER|wx.ALL, 1)
        layout_sizer.Add(self.target_control, 0, wx.ALIGN_LEFT|wx.EXPAND|wx.ALL, 1)

        self.SetSizer(layout_sizer)
        layout_sizer.Fit(self)

        self.Bind(wx.EVT_BUTTON, self.FormateJson, self.json_format_btn)
        self.Bind(wx.EVT_BUTTON, self.ReadContent, self.xml_format_btn)

    def ReadContent(self, event):
        event_object = event.GetEventObject()
        label = event_object.GetLabel()
        print(label)
        self.target_control.SetValue(self.source_control.GetValue())

    # 格式化Json
    def FormateJson(self, event):
        source_str = {'age':'34', 'name':'allen', 'address':'shanghai', 'company':{'name':'nuoya', 'address':'hangkong'}}
        print(type(source_str))
        self.format_json = json.dumps(source_str,
                        sort_keys=True, indent=4, separators=(',',':'))
        loads = json.loads(self.format_json)
        print(type(loads))
        print(loads)
        print(self.format_json);
        print(source_str['age'])

        self.target_control.SetValue(self.format_json)





    # def FormatXml(self, event):


app = wx.App(False)
frame = MainWindow(None, 'Small editor')
frame.Show()
app.MainLoop()


